package com.lixw.springai.controller;

import com.lixw.springai.utils.DateTimeTools;
import com.lixw.springai.utils.ExpressTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @ClassName: ToolCallingController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/26 20:19
 **/
@RestController
public class ToolCallingController {
    @Resource
    private ChatModel chatModel;

    @Resource
    private ChatClient chatClient;


    @GetMapping("/toolCall/chat")
    public String chat(@RequestParam(value = "msg",defaultValue = "你是谁，现在几点了,再帮我查一下快递1001") String msg) {
        //1.将工具注册到工具集
        ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools(), new ExpressTools());
        //2.将工具集设置到 仅本次 对话的选项中
        ToolCallingChatOptions options = ToolCallingChatOptions.builder()
                .toolCallbacks(tools)
                .build();

        //提示词
        Prompt prompt = new Prompt(msg, options);

        return chatModel.call(prompt).getResult().getOutput().getText();
    }

    @GetMapping("/toolCall/chat2")
    public Flux<String> chat2(@RequestParam(value = "msg",defaultValue = "你是谁，现在几点了") String msg) {
        return chatClient.prompt(msg).tools(new DateTimeTools()).stream().content();
    }
}